import http from 'wechat-http'
let request = http
request.baseURL = "https://live-api.itheima.net"
request.intercept.response = res => {
  if (res.data.code === 401) {
    wx.getStorage({
      key: 'refreshToken',
      async success(resToken) {
        await http({
          url: "/refreshToken",
          method: "post",
          header: {
            "Authorization": 'Bearer ' + resToken.data
          }
        }).then(res => {
          if (res.data.code === 10000) {
            wx.setStorage({
              key: 'token',
              data: res.data.data.token
            })
            wx.setStorage({
              key: 'refreshToken',
              data: res.data.data.refreshToken
            })
          }
        })

      }
    })
  }
  return res
}
export default request